package com.hzh.flink.core

import org.apache.flink.streaming.api.scala._

object Demo1WordCount {
  def main(args: Array[String]): Unit = {
    /**
     * 1、创建flink的环境
     *
     */
    val env: StreamExecutionEnvironment = StreamExecutionEnvironment.getExecutionEnvironment

    //设置flink的并行度
    env.setParallelism(1)

    /**
     * 2、读取数据
     */

    val lines: DataStream[String] = env.socketTextStream("master", 8888)

    /**
     * 3、统计单词
     */

    val wordsDS: DataStream[String] = lines.flatMap(_.split(","))

    val kvDS: DataStream[(String, Int)] = wordsDS.map((_, 1))

    val groupDS: KeyedStream[(String, Int), String] = kvDS.keyBy(kv => kv._1)

    val resultDS: DataStream[(String, Int)] = groupDS.sum(1)

    /**
     * 4、查看结果
     */

    resultDS.print()

    /**
     * 启动flink
     */

    env.execute()


  }
}
